import { defineMock } from "umi";

type Product = {
    id: string;
    name: string;
};

const products: Product[] = [
    { id: "1", name: "Product 1" },
    { id: "2", name: "Product 2" },
    { id: "3", name: "Product 3" },
    { id: "4", name: "Product 4" },
    { id: "5", name: "Product 5" },
    { id: "6", name: "Product 6" },
    { id: "7", name: "Product 7" },
    { id: "8", name: "Product 8" },
    { id: "9", name: "Product 9" },
    { id: "10", name: "Product 10" },
    { id: "11", name: "Product 11" }
];

export default defineMock({
    "GET /api/products": (_, res) => {
        res.send({
            code: 200,
            message: "success",
            data: products,
        });
    },
    "DELETE /api/products/:id": (req, res) => {
        const { id } = req.params;
        const index = products.findIndex((product) => product.id === id);
        if (index !== -1) {
            products.splice(index, 1);
            res.send({
                code: 200,
                message: "success",
            });
        } else {
            res.send({
                code: 404,
                message: "Product not found",
            });
        }
    },
    "POST /api/products": (req, res) => {
        const { name } = req.body;
        const newProduct = {
            id: (products.length + 1).toString(),
            name,
        };
        products.push(newProduct);
        res.send({
            code: 200,
            message: "success",
            data: newProduct,
        });
    }
});


